<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>授权管理</title>
<link rel="stylesheet" href="../webjars/bootstrap/3.3.7-1/css/bootstrap.css" />
<link rel="stylesheet" href="../static/jquery-easyui-1.5.5.3/themes/material-teal/easyui.css" />
<link rel="stylesheet" href="../static/jquery-easyui-1.5.5.3/themes/icon.css" />
<script src="../static/jquery-easyui-1.5.5.3/jquery.min.js"></script>
<script src="../static/jquery-easyui-1.5.5.3/jquery.easyui.min.js"></script>
<script src="../static/jquery-easyui-1.5.5.3/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
	$(function(){
		$("#t_user").datagrid({
			idField: 'id',
			title: '授权列表',
			fitColumns: true,
			width: '100%',
			striped: true,
			loadMsg: '数据正在加载...',
			rownumbers: true,
			url: 'list',
			frozenColumns: [
				[
					{
						//加多选按钮
						field: 'ck',width: 50,checkbox: true
					}
				]
			],
			columns: [
				[
					{
						field: 'id',title: '序号',width: 50,align: 'center'
					},{
						field: 'Name',title: '租户名称',width: 100,align: 'center',
						formatter: function(value, rec){   //easyui取嵌套类对象的值
							return rec.lessee.name;
						}
						
					},{
						field: 'month',title: '使用月份',width: 50,align: 'center'
					},{
						field: 'beginDate',title: '开始日期',width: 150,align: 'center'
					
					},{
						field: 'overDate',title: '结束日期',width: 150,align: 'center'
					},{
						field: 'cost',title: '缴纳费用',width: 100,align: 'center'
						
					},{
						field: 'Unit',title: '租户管理员',width: 100,align: 'center',
						formatter: function(value, rec){ 
							return rec.lessee.lesseeAdmin.unit;
						}
					}
				]
			],
			pagination: true,
			pageSize: 5,
			pageList: [ 5, 10, 20, 25, 50],
			toolbar: [
				{
					text: '修改授权',
					iconCls: 'icon-edit',
					handler: function(){
						var arr = $('#t_user').datagrid('getSelections');
						if(arr.length != 1){
							$.messager.show({
								title: '提示信息',
								msg: '选中一行记录进行修改！'
							});
						}else{
							//打开窗口
							$('#mydialog').dialog('open');
							//清空form表单
							//$('#myform').get(0).reset();
							$('#myform input').val("");
							$('#myform').form('load', {  //把选中的数据放入到form表单中
								id:arr[0].id,
								beginDate:arr[0].beginDate,
								overDate:arr[0].overDate,
								month:arr[0].month,
								cost:arr[0].cost,
								lessee:arr[0].lessee.id
							});
						}
					}
				}
			]
		});
		
		//提交表单方法
		$("#btn1").click(function(){
			if($('#myform').form('validate')){
				$.ajax({
					type: 'post',
					url: 'save',
					cache: false,   //清除缓存
					data: $('#myform').serialize(),
					//dataType: 'json',
					success: function(data){
						//关闭窗口
						$("#mydialog").dialog('close');
						//reload刷新datagrid表格
						$('#t_user').datagrid('reload');
						//提示信息
						$.messager.show({
							title: '提示信息',
							msg: '操作成功！'
						});
					}
				});
			}else{
				$.messager.show({
					title: '提示信息',
					msg: '数据验证不通过，不能保存'
				});
			}
		});
		
		//关闭窗口按钮
		$("#btn2").click(function(){
			$("#mydialog").dialog('close');
		});
		
		$("#beginDate, #overDate").datebox({
			width: 200,
			editable: false
		});
		
		//查询
		$('#searchbtn').click(function(){
			$('#t_user').datagrid('load', serializeForm($('#mysearch').form()));
		});
		//清除查询
		$('#clearbtn').click(function(){
			//清空form表单
			$('#mysearch').form('clear');
			$("#t_user").datagrid('load', {});
		});
	});
	//js方法，序列化表单
	function serializeForm(form){
		var obj = {};
		$('#id').val("1");
		$.each(form.serializeArray(), function(index){
			if(obj[this['name']]){
				obj[this['name']] = obj[this['name']] + ',' + this['value'];
			}else{
				obj[this['name']] = this['value'];
			}
		});
		return obj;
	}
</script>
<style type="text/css">
	#ss{
	
		margin: 20px;
	}
</style>
</head>
<body>
	<div id="ss"> 
		<form id="mysearch" method="post">
			<input type="hidden" name = "id" id="id">
			<div class="col-md-3">			
				<div class="input-group">
					<span class="input-group-addon">租户名称</span>
					<input type="text" class="form-control" name="name" placeholder="租户名称">
				</div>
			</div>
			<div class="col-md-3">			
				<div class="input-group">
					<span class="input-group-addon">租户管理员</span>
					<input type="text" class="form-control" name="unit" placeholder="租户管理员">
				</div>
			</div>
			<div class="btn-group">
				<button id="searchbtn" class="btn btn-success"><span class="glyphicon glyphicon-search"></span> 查询</button>
				<button id="clearbtn" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span> 清空</button>
			
			</div>
		</form>
	</div>
	<table id = "t_user"></table>
	
	<div id = "mydialog" title="修改授权" modal="true"  draggable=false class="easyui-dialog" closed=true style="width:300px;">
		<form action="" method="post" id = "myform">
			<input type="hidden" name = "id" value="">
			<input type="hidden" name = "lessee" value="">
			<table>
				<tr>
					<td>开始日期：</td>
					<td><input type="text" name = "beginDate" id="beginDate" class="easyui-validatebox" required=true value="" ></td>
				</tr>
				<tr>
					<td>结束日期：</td>
					<td><input type="text" name = "overDate" id="overDate" class="easyui-validatebox" required=true value="" ></td>
				</tr>
				<tr>
					<td>使用期限（月）：</td>
					<td><input type="text" name = "month" id="month" class="easyui-validatebox" required=true value="" ></td>
				</tr>
				<tr>
					<td>缴纳费用：</td>
					<td><input type="text" name = "cost" class="easyui-validatebox" required=true missingMessage="缴纳费用不能为空" value="" ></td>
				</tr>
				<tr>
					<td colspan="2" align="center">
						<a id="btn1" class="easyui-linkbutton">确定</a>
						<a id="btn2" class="easyui-linkbutton">关闭</a>
					</td>
				</tr>
			</table>
		</form>
	</div>
</body>
</html>